package com.example.contact_recover.vv;

import android.app.AlertDialog;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.contact_recover.R;
import com.example.contact_recover.models.ContactsInfo;

import java.util.List;

// 联系人的展示列表
public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ContactHolder> {

    List<ContactsInfo> contactsInfoList;

    public List<ContactsInfo> getContactsInfoList() {
        return contactsInfoList;
    }

    public void setContactsInfoList(List<ContactsInfo> contactsInfoList) {
        this.contactsInfoList = contactsInfoList;
    }

    public ContactAdapter(List<ContactsInfo> contactsInfoList) {
        this.contactsInfoList = contactsInfoList;
    }

    public ContactAdapter() {
    }

    @NonNull
    @Override
    public ContactHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_contact_item, null);
        return new ContactHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ContactHolder holder, int position) {

        ContactsInfo contactsInfo = contactsInfoList.get(position);

        holder.textName.setText(contactsInfo.getName());
        holder.textPhone.setText(contactsInfo.getNumber());
        // 设置点击事件
        holder.textPhone.setOnClickListener((v)->{

            View view = LayoutInflater.from(v.getContext()).inflate(R.layout.select_view, null);

            TextView tv_call = view.findViewById(R.id.tv_call);
            tv_call.setOnClickListener((vcall)->{
                Intent intent = new Intent();

                Uri data = Uri.parse("tel:" + contactsInfo.getNumber());

                intent.setData(data);
                intent.setAction(Intent.ACTION_CALL);
                vcall.getContext().startActivity(intent);
            });

            TextView tv_message = view.findViewById(R.id.tv_message);

            tv_message.setOnClickListener((vm)->{

                // todo
                // 发送短信服务
                Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:"+contactsInfo.getNumber()));
                intent.putExtra("sms_body", "ceshi");
               vm.getContext(). startActivity(intent);
            });

            AlertDialog alertDialog = new AlertDialog.Builder(v.getContext()).setMessage("select more you want to do ?" + contactsInfo.getNumber()).

                    setView(view)

                    .create();

            alertDialog.show();
        });

    }

    @Override
    public int getItemCount() {
        if(contactsInfoList == null){
            return 0 ;
        }
        return contactsInfoList.size();
    }

    public class ContactHolder extends RecyclerView.ViewHolder {

        TextView textPhone;
        TextView textName;

        public ContactHolder(View view) {
            super(view);


            textName = view.findViewById(R.id.contact_tv_show_name);
            textPhone = view.findViewById(R.id.contact_tv_show_phone);

        }
    }
}
